package com.zx.chatroom.service;

import com.zx.chatroom.po.*;
import com.zx.framework.core.dao.IBaseDAO;
import com.zx.framework.core.exception.ServiceException;
import com.zx.framework.core.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Component("loginService")
@Transactional
public class LoginService {

	private static final Logger LOGGER = Logger.getLogger(LoginService.class);
	@Resource
	private IBaseDAO baseDAO;
	
	/**
	 * 获取User详细信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public User findUserById(String id){
		if(StringUtil.isEmpty(id)){
			ServiceException exception = new ServiceException("User ID must be not null!");
			LOGGER.error(exception.getMessage());
			throw exception;
		}
		return baseDAO.findById(id, User.class);
	}
	
	/**
	 * 获取UserExt详细信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public UserExt findUserExtById(String id){
		if(StringUtil.isEmpty(id)){
			ServiceException exception = new ServiceException("UserExt ID must be not null!");
			LOGGER.error(exception.getMessage());
			throw exception;
		}
		return baseDAO.findById(id, UserExt.class);
	}
	
	/**
	 * 获取Dept详细信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Dept findDeptById(String id){
		if(StringUtil.isEmpty(id)){
			ServiceException exception = new ServiceException("Dept ID must be not null!");
			LOGGER.error(exception.getMessage());
			throw exception;
		}
		return baseDAO.findById(id, Dept.class);
	}
	
	public List<AutoReply> getAllAutoReply(){
		return baseDAO.findAll("AutoReply");
	}
	
	public Integer findScoreInfoByfromUserAndtoUser(String fromUser,String toUser){
		String sql = "select count(s.id) from k_scoreinfo s where s.fromuser = ? and s.touser = ?";
		return baseDAO.getSQLUnique(sql, BigDecimal.class, fromUser, toUser).intValue();
	}
	
	public void saveScoreInfo(ScoreInfo scoreInfo){
		baseDAO.save(scoreInfo);
	}
	
	public ScoreInfo findScoreInfoById(String id){
		return baseDAO.findById(id, ScoreInfo.class);
	}
	
	public void updateScoreInfo(ScoreInfo scoreInfo){
		baseDAO.update(scoreInfo);
	}
}
